package com.colorlight.cloud.mqtt.base.entity;

import com.colorlight.cloud.common.exception.ParameterException;
import lombok.Builder;
import lombok.Data;

import java.util.Objects;

/**
 * @author Demon
 * 监听主题细节
 * 支持  topic/#  和 topic/sub-topic  不同级别QOS
 */
@Data
@Builder
public class TopicDetail {
	private String topic;
	private QosEnum qosEnum;
	
	TopicDetail(String topic) {
		/**
		 * 默认监听为 0
		 * */
		this(topic, QosEnum.QOS_1);
	}
	
	TopicDetail(String topic, Integer qosValue) {
		this(topic, QosEnum.getQos(qosValue));
	}
	
	TopicDetail(String topic, QosEnum qosEnum) {
		if (topic == null || topic.isEmpty()) {
			throw new ParameterException("主题不能为空");
		}
		this.topic = topic;
		this.qosEnum = qosEnum;
	}
	
	@Override
	public boolean equals(Object o) {
		if (Objects.isNull(o)) {
			return false;
		}
		if (o == this) {
			return true;
		}
		
		if (o instanceof TopicDetail) {
			TopicDetail topicDetail;
			topicDetail = (TopicDetail) o;
			return this.topic.equals(topicDetail.getTopic());
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return this.topic.hashCode();
	}
	
}
